<?php
include_once('simple_html_dom.php');
include_once ('common.php');
class KqxsVn {

	private $crawlUrl;
	private $xpath='div#p-middle div.colwrap table tr td table tr td table';
	private $titleXpath='div#p-middle div.colwrap table tr td table tr td.main_box_t_r h3 span.icon';
	public function __construct( $url ) {
		$this->crawlUrl=$url;
	}

	// Get data for comparison only
	function crawl() {
		
		$result = $this->rawCrawl();
		$rawDetail=$result['rawdetail'];		
		$detail=array();
		
		// Other prizes
		foreach($rawDetail as $item){
			$prizes=explode('-', $item);
			$prizes=array_map('trim', $prizes);
			$detail=array_merge($detail,$prizes);
		}
		
		$result['detail']=$detail;
		return $result;
	}

	// Raw data, can be used to insert to database
	function rawCrawl()	{
		$result = array('open_date'=>'','rawdetail'=>array());
		$rawDetail=array();
		// get DOM from URL or file
		$context=stream_context_create(array('http'=>
				array(
		        'timeout' => 1200 // 20 minutes
				)
		));
		$html = file_get_html($this->crawlUrl,false, $context);

		// Get open_date
		$titleSpanList = $html->find($this->titleXpath);
		$titleSpan=$titleSpanList[0];
		$title = trim($titleSpan->innertext);
		$pattern='/\d\d-\d\d-\d\d\d\d/';
		preg_match($pattern, $title, $matches, PREG_OFFSET_CAPTURE);
			
		$result['open_date']=$matches[0][0];
		$result['open_date']=nomalizeDate($result['open_date']);
		
		// Get result detail
		$mainTableList =$html->find($this->xpath);
		$mainTable=$mainTableList[0];
			
		// Special prize
		$specialPrice = $mainTable->find("th.sodacbiet");
		$rawDetail[]=$specialPrice[0]->text();
			
		// Other prizes
		foreach($mainTable->find('td.value') as $td){
			$prize=trim($td->text());
			$rawDetail[]=$prize;
		}

		$result['rawdetail']=$rawDetail;
		
		return $result;
	}
}

?>